package com.zzl.bonds.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzl.bonds.ticket.dao.TicketFinanceDataDao;
import com.zzl.bonds.ticket.dto.TickefinanceDataAddDto;
import com.zzl.bonds.ticket.dto.TickefinanceDataUpdateDto;
import com.zzl.bonds.ticket.dto.TicketFinanceDataTypeDto;
import com.zzl.bonds.ticket.entity.TicketFinanceDataEntity;
import com.zzl.bonds.ticket.service.TicketFinanceDataService;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.serurity.entity.User;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Query;
import com.zzl.general.common.utils.UuidUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 融资资料表
 *
 * @author gull
 * @date 2022-07-06 13:43:52
 */
@Service("ticketFinanceDataService")
public class TicketFinanceDataServiceImpl extends ServiceImpl<TicketFinanceDataDao, TicketFinanceDataEntity> implements TicketFinanceDataService {

    /**
     * 分页查询
     *
     * @param params 查询参数
     * @return 分页数据
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TicketFinanceDataEntity> page = this.page(
                new Query<TicketFinanceDataEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 新增
     *
     * @param tickefinanceDataAddDto 新增实体
     * @param user                   当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void saveTickefinanceData(TickefinanceDataAddDto tickefinanceDataAddDto, User user) throws SysException {
        TicketFinanceDataEntity ticketFinanceDataEntity = new TicketFinanceDataEntity();
        BeanUtils.copyProperties(tickefinanceDataAddDto, ticketFinanceDataEntity);
        ticketFinanceDataEntity.setCreatorId(user.getId());
        if (!this.save(ticketFinanceDataEntity)) {
            throw new SysException("A00001", "保存失败！");
        }
    }

    /**
     * 修改
     *
     * @param tickefinanceDataUpdateDto 修改实体
     * @param user                      当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void updateTickefinanceDataById(TickefinanceDataUpdateDto tickefinanceDataUpdateDto, User user) throws SysException {
        TicketFinanceDataEntity ticketFinanceDataEntity = new TicketFinanceDataEntity();
        BeanUtils.copyProperties(tickefinanceDataUpdateDto, ticketFinanceDataEntity);
        ticketFinanceDataEntity.setUpdaterId(user.getId());
        if (!this.updateById(ticketFinanceDataEntity)) {
            throw new SysException("A00002", "修改失败！");
        }
    }

    /**
     * 融资补充资料保存
     *
     * @param financeDataDtoList 融资资料
     * @param id                 融资申请id
     * @param user               用户
     */
    @Override
    public void saveTicketBatchfinanceData(List<TicketFinanceDataTypeDto> financeDataDtoList, String id, User user) {
        List<TicketFinanceDataEntity> list = new ArrayList<>(financeDataDtoList.size());
        financeDataDtoList.stream().forEach(x -> {
            TicketFinanceDataEntity ticketFinanceDataEntity = new TicketFinanceDataEntity();
            BeanUtils.copyProperties(x, ticketFinanceDataEntity);
            ticketFinanceDataEntity.setCreatorId(user.getId());
            ticketFinanceDataEntity.setUpdaterId(user.getId());
            ticketFinanceDataEntity.setTicketFinanceId(id);
            ticketFinanceDataEntity.setId(UuidUtils.UUID());
            list.add(ticketFinanceDataEntity);
        });
        this.saveBatch(list);

    }

}